<template>
  <div class="mytasks">
    <div class="title-box">
        <h2><slot></slot></h2>
        <span>
            {{isfinish ? tasks.length - $store.getters.num : $store.getters.num}}
        </span>
    </div>
    <div class="tasks-box">
        <ul>
            <li v-for="(item, i) in tasks" v-show="isfinish === item.check" :key="item.id">
                <div class="task">
                    <input type="checkbox" v-model="item.check">
                    <div @click="exit(item, i)">
                        <span v-if="!item.isExit">{{item.task}}</span>
                        <input :class="['exitInput', 'exit' + i]" v-else type="text" v-model="item.task" @blur="blur(item)">
                    </div>
                </div>
                <button @click="del(i)">-</button>
            </li>
        </ul>
    </div>
 </div>
</template>

<script>
import { useStore } from 'vuex'
import { watch, nextTick } from 'vue'
export default {
    props: {
        isfinish: Boolean
    },
    setup() {
        // 导入vuex的数据和方法
        const store = useStore()
        const tasks = store.state.tasks

        function del(i) {
            tasks.splice(i, 1)
            store.commit('changeTasks', tasks)
        }

        // 编辑完成后 输入框失去焦点保存任务内容
        function blur(item) {
            item.isExit = false
            store.commit('changeTasks', tasks)
        }
        // 点击任务出发的事件
        function exit(item, i) {
            item.isExit = true // 当编辑状态变化后会像dom树中添加input元素
            nextTick(() => {  // 因为渲染元素时异步的 所以 如果不适用nextTick 点击获取不到input元素
            // 所以这里需要使用nextTick nextTick的作用时在dom更新或者打补丁完成后立即出发的一个函数
                let classInput = '.exit' + i
                // console.log(classInput);
                let input = document.querySelector(classInput)
                // console.log(inputs);
                input.focus() // 编辑状态下使input框获取焦点
                input.select() // 让input框中的内容处于选中状态
            })
            
        }

        watch(() => tasks, (to) => {
            // console.log(to);
            store.commit('changeTasks', to)
        }, {
            deep: true
        })
        return {
            tasks,
            del,
            blur,
            exit
        }
    }
 }
</script>

<style scoped lang="scss">
ul{
    list-style: none;
    padding: 0;
    margin: 0;
}
.mytasks {
    width: 800px;
    margin: 20px auto;
    .title-box {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        span {
            width: 20px;
            height: 20px;
            text-align: center;
            line-height: 20px;
            background-color: #E6E6FA;
            border-radius: 20px;
            font-weight: 600;
        }
    }
    .tasks-box li {
        display: flex;
        width: 100%;
        justify-content: space-between;
        background-color: #fff;
        border-left: 5px solid #629A9C;
        padding-left: 15px;
        height: 40px;
        align-items: center;
        border-radius: 6px;
        padding-right: 15px;
        margin-top: 20px;
        input {
            width: 24px;
            height: 24px;
            margin-right: 10px;
        }
        .exitInput {
            width: 250px;
        }
        .task {
            display: flex;
            align-items: center;
            div {
                cursor: move;
            }
            
        }
        button {
            width: 27px;
            height: 24px;
            border-radius: 14px;
            border: 6px double #FFF;
            background: #CCC;
            line-height: 9px;
            text-align: center;
            color: #FFF;
            font-weight: bold;
            font-size: 14px;
            cursor: pointer;
        }
    }
}

</style>